package com.raidpoints.client;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.ValuePicker;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.TimeZone;
import com.google.gwt.user.client.ui.TextBox;
import com.raidpoints.client.RaidPoints.HasRedraw;
import com.summatech.gwt.client.HourMinutePicker;
import com.summatech.gwt.client.HourMinutePicker.PickerFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.CheckBox;

public class NewEventBox extends DialogBox {
	RaidPoints rp;
	ListBox gameBox;
	ListBox destBox;
	ListBox secdestBox;
	ListBox groupBox;
	ArrayList<Integer> poolid;
	HashMap<String,ArrayList<pointspool>> charterList;
	TextArea noteBox;
	IntegerBox integerBox;
	event evt;
	CheckBox chckbxCancel;
	TextBox startBox; 
	TextBox endBox;
	Button btnAdd;
	Button btnSplit;
	DateBox dateBox;
	Label lblTimesToReoccur;
	PointsPage pp;
	ArrayList<Integer> chartergid;
	ArrayList<String> charterpoolname;
	ArrayList<String> gameboxitems;
	ArrayList<pointspool> poolitems;
	AsyncCallback<String> gameCallback;
	private TextBox genBox;
	boolean isgen;
	private LayoutPanel layoutPanel;
	private Label lblSecDestination;
	private Label lblPointsPool;
	private Label lblNewLabel;

	public NewEventBox(RaidPoints mrp){
		this(mrp,null,null,null);
	}

	public NewEventBox(RaidPoints mrp, event mevt){
		this(mrp,null,null,mevt);
	}

	public NewEventBox(RaidPoints mrp, java.util.Date dt, event mevt){
		this(mrp,null,dt,mevt);
	}


	/**
	 * @wbp.parser.constructor
	 */
	public NewEventBox(RaidPoints mrp, PointsPage mpp, java.util.Date dt, event mevt) {
		this.rp = mrp;
		evt = mevt;
		pp = mpp;
		isgen = false;
		poolid = new ArrayList<Integer>();
		chartergid = new ArrayList<Integer>();
		charterpoolname = new ArrayList<String>();
		gameboxitems = new ArrayList<String>();
		poolitems = new ArrayList<pointspool>();
		if(evt == null) setHTML("Add Event");
		else setHTML("Modify Event");

		layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("333px", "551px");

		lblNewLabel = new Label("Prim. Destination:");
		layoutPanel.add(lblNewLabel);
		layoutPanel.setWidgetLeftWidth(lblNewLabel, 22.0, Unit.PX, 109.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNewLabel, 131.0, Unit.PX, 16.0, Unit.PX);

		destBox = new ListBox();
		layoutPanel.add(destBox);
		destBox.setWidth("157px");
		layoutPanel.setWidgetLeftWidth(destBox, 141.0, Unit.PX, 157.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(destBox, 123.0, Unit.PX, 24.0, Unit.PX);

		dateBox = new DateBox();
		dateBox.setFormat(new DefaultFormat(DateTimeFormat.getFormat("y MMM d")));
		layoutPanel.add(dateBox);
		layoutPanel.setWidgetLeftWidth(dateBox, 125.0, Unit.PX, 157.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(dateBox, 277.0, Unit.PX, 32.0, Unit.PX);
		if(dt != null) dateBox.setValue(dt);


		Label lblNewLabel_1 = new Label("Date:");
		layoutPanel.add(lblNewLabel_1);
		layoutPanel.setWidgetLeftWidth(lblNewLabel_1, 22.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNewLabel_1, 293.0, Unit.PX, 16.0, Unit.PX);

		Label lblStart = new Label("Start:");
		layoutPanel.add(lblStart);
		layoutPanel.setWidgetLeftWidth(lblStart, 22.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblStart, 332.0, Unit.PX, 16.0, Unit.PX);

		startBox = new TextBox();
		startBox.setMaxLength(5);
		startBox.setText("12:00");
		startBox.setWidth("55px");
		layoutPanel.add(startBox);
		layoutPanel.setWidgetLeftWidth(startBox, 66.0, Unit.PX, 65.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(startBox, 316.0, Unit.PX, 32.0, Unit.PX);

		Label lblEnd = new Label("End:");
		layoutPanel.add(lblEnd);
		layoutPanel.setWidgetLeftWidth(lblEnd, 191.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblEnd, 332.0, Unit.PX, 16.0, Unit.PX);

		endBox = new TextBox();
		endBox.setMaxLength(5);
		endBox.setText("13:00");
		layoutPanel.add(endBox);
		endBox.setWidth("55px");
		layoutPanel.setWidgetLeftWidth(endBox, 233.0, Unit.PX, 65.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(endBox, 315.0, Unit.PX, 32.0, Unit.PX);


		Label lblGroup = new Label("Group:");
		layoutPanel.add(lblGroup);
		layoutPanel.setWidgetLeftWidth(lblGroup, 22.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblGroup, 15.0, Unit.PX, 16.0, Unit.PX);

		lblPointsPool = new Label("Game - Points Pool:");
		layoutPanel.add(lblPointsPool);
		layoutPanel.setWidgetLeftWidth(lblPointsPool, 98.0, Unit.PX, 149.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblPointsPool, 45.0, Unit.PX, 16.0, Unit.PX);

		groupBox = new ListBox();
		layoutPanel.add(groupBox);
		layoutPanel.setWidgetLeftWidth(groupBox, 141.0, Unit.PX, 157.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(groupBox, 15.0, Unit.PX, 24.0, Unit.PX);

		btnAdd = new Button("Add");
		btnAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(!timevalid(startBox.getText()) || !timevalid(endBox.getText())){
					Window.alert("Start and/or end time invald. Format: hh:mm (24-hour)");
					return;
				}
				if(dateBox.getTextBox().getText().equals("")){
					Window.alert("Must enter date");
					return;
				}
				if(groupBox.getItemText(groupBox.getSelectedIndex()).equals("General") && genBox.getText().equals("")){
					Window.alert("Must enter title");
					return;
				}

				if(evt == null){
					AsyncCallback<ArrayList<event>> addEventCallback = new AsyncCallback<ArrayList<event>>(){

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(ArrayList<com.raidpoints.client.event> result) {
							if(result==null) return;
							rp.dataProvider.getList().addAll(result);
							Collections.sort(rp.dataProvider.getList(),rp.tsComp);
							Collections.reverse(rp.dataProvider.getList());		
							for(event e: result){
								((RaidPoints.HasRedraw) rp.navmap.get(rp.eventItem)).AddEvent(e);
							}
						}

					};
					event te = new event();
					if(groupBox.getItemText(groupBox.getSelectedIndex()).equals("General")){
						te.game = "";
						te.dest = "";
						String rn = noteBox.getText();
						if(rn.equals("")) rn = "Description";
						te.note = genBox.getText() + ":;:" + rn;				    
					}
					else{
						te.dest  = destBox.getItemText(destBox.getSelectedIndex());
						te.game = gameboxitems.get(gameBox.getSelectedIndex());
						te.group = groupBox.getItemText(groupBox.getSelectedIndex());
						te.poolid = poolid.get(gameBox.getSelectedIndex());
						for(int i = 0; i < secdestBox.getItemCount(); i++){
							if(secdestBox.isItemSelected(i)) te.secdest = te.secdest + "," + secdestBox.getItemText(i);
						}
						if(te.secdest.length() > 0) te.secdest = te.secdest.substring(1);
						te.note = noteBox.getText();
					}
					java.util.Date dt = dateBox.getValue();
					int sh = Integer.parseInt(startBox.getText().split(":")[0]);
					int sm = Integer.parseInt(startBox.getText().split(":")[1]);
					int eh = Integer.parseInt(endBox.getText().split(":")[0]);
					int em = Integer.parseInt(endBox.getText().split(":")[1]);

					te.starttime = new NCDate(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate(), sh, sm);
					te.endtime = new NCDate(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate(), eh, em);

					//		te.starttime = new Timestamp(dt.getYear(),dt.getMonth(),dt.getDate(),sh,sm,0,0);
					//		te.endtime = new Timestamp(dt.getYear(),dt.getMonth(),dt.getDate(),eh,em,0,0);
					rp.rpsvc.AddEvent(te, integerBox.getValue()+1,addEventCallback);
				}
				else{
					AsyncCallback<event> updateCallback = new AsyncCallback<event>(){

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(com.raidpoints.client.event result) {
							for(event e : rp.dataProvider.getList()){
								if(e.eid == evt.eid){
									for(int i = 0; i < rp.eventItem.getChildCount(); i++){
										if(((PointsPage)rp.navmap.get(rp.eventItem.getChild(i))).evt.eid == evt.eid){
											if(result == null) rp.RemoveTab(rp.eventItem.getChild(i));
											else ((PointsPage)rp.navmap.get(rp.eventItem.getChild(i))).Refresh();
											break;
										}
									}
									boolean rc  = false;
									if(rp.navmap.get(rp.eventItem) instanceof RaidCalendar){
										rc = true;
										((RaidCalendar) rp.navmap.get(rp.eventItem)).RemoveEvent(evt);
									}	
									rp.dataProvider.getList().remove(e);
									if(result != null){
										rp.dataProvider.getList().add(result);
										if(rc) ((RaidCalendar) rp.navmap.get(rp.eventItem)).AddEvent(evt);
									}	
									((HasRedraw) rp.navmap.get(rp.eventItem)).Redraw();
									break;
								}
							}

							hide();
						}

					};
					if(groupBox.getItemText(groupBox.getSelectedIndex()).equals("General")){
						evt.game = "";
						evt.dest = "";
						String rn =  noteBox.getText();
						if(rn.equals("")) rn = "Description";
						evt.note = genBox.getText() + ":;:" + rn;
					}
					else{
						evt.dest  = destBox.getItemText(destBox.getSelectedIndex());
						evt.game = gameboxitems.get(gameBox.getSelectedIndex());
						evt.group = groupBox.getItemText(groupBox.getSelectedIndex());
						evt.poolid = poolid.get(gameBox.getSelectedIndex());
						evt.secdest = "";
						for(int i = 0; i < secdestBox.getItemCount(); i++){
							if(secdestBox.isItemSelected(i)) evt.secdest = evt.secdest + "," + secdestBox.getItemText(i);
						}
						if(evt.secdest.length() > 0) evt.secdest = evt.secdest.substring(1);
						evt.note = noteBox.getText();
					}
					java.util.Date dt = dateBox.getValue();
					int sh = Integer.parseInt(startBox.getText().split(":")[0]);
					int sm = Integer.parseInt(startBox.getText().split(":")[1]);
					int eh = Integer.parseInt(endBox.getText().split(":")[0]);
					int em = Integer.parseInt(endBox.getText().split(":")[1]);
					evt.starttime = new NCDate(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate(), sh, sm);
					evt.endtime = new NCDate(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate(), eh, em);
					rp.rpsvc.UpdateEvent(evt, chckbxCancel.getValue(), updateCallback);
				}
				hide();
			}
		});
		layoutPanel.add(btnAdd);
		layoutPanel.setWidgetLeftWidth(btnAdd, 50.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnAdd, 513.0, Unit.PX, 28.0, Unit.PX);

		Button btnCancel = new Button("Cancel");
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		layoutPanel.add(btnCancel);
		layoutPanel.setWidgetLeftWidth(btnCancel, 166.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnCancel, 513.0, Unit.PX, 28.0, Unit.PX);

		gameBox = new ListBox();
		gameBox.setWidth("276px");
		layoutPanel.add(gameBox);
		layoutPanel.setWidgetLeftWidth(gameBox, 22.0, Unit.PX, 300.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(gameBox, 77.0, Unit.PX, 24.0, Unit.PX);

		Label lblNote = new Label("Note:");
		layoutPanel.add(lblNote);
		layoutPanel.setWidgetLeftWidth(lblNote, 22.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNote, 405.0, Unit.PX, 16.0, Unit.PX);

		noteBox = new TextArea();
		noteBox.setWidth("232px");
		layoutPanel.add(noteBox);
		layoutPanel.setWidgetLeftWidth(noteBox, 66.0, Unit.PX, 245.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(noteBox, 369.0, Unit.PX, 73.0, Unit.PX);

		integerBox = new IntegerBox();
		integerBox.setVisibleLength(3);
		integerBox.setText("0");
		layoutPanel.add(integerBox);
		layoutPanel.setWidgetLeftWidth(integerBox, 177.0, Unit.PX, 46.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(integerBox, 448.0, Unit.PX, 26.0, Unit.PX);

		lblTimesToReoccur = new Label("Times to Reoccur:");
		layoutPanel.add(lblTimesToReoccur);
		layoutPanel.setWidgetLeftWidth(lblTimesToReoccur, 50.0, Unit.PX, 125.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblTimesToReoccur, 458.0, Unit.PX, 16.0, Unit.PX);

		chckbxCancel = new CheckBox("Cancel");
		layoutPanel.add(chckbxCancel);
		layoutPanel.setWidgetLeftWidth(chckbxCancel, 50.0, Unit.PX, 109.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(chckbxCancel, 488.0, Unit.PX, 19.0, Unit.PX);

		lblSecDestination = new Label("Secondary Destinations:");
		layoutPanel.add(lblSecDestination);
		layoutPanel.setWidgetLeftWidth(lblSecDestination, 22.0, Unit.PX, 153.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblSecDestination, 159.0, Unit.PX, 16.0, Unit.PX);

		secdestBox = new ListBox();
		secdestBox.setMultipleSelect(true);
		secdestBox.setWidth("248px");
		layoutPanel.add(secdestBox);
		layoutPanel.setWidgetLeftWidth(secdestBox, 50.0, Unit.PX, 295.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(secdestBox, 181.0, Unit.PX, 90.0, Unit.PX);
		secdestBox.setVisibleItemCount(5);

		btnSplit = new Button("Split");
		layoutPanel.add(btnSplit);
		layoutPanel.setWidgetLeftWidth(btnSplit, 166.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnSplit, 479.0, Unit.PX, 28.0, Unit.PX);

		genBox = new TextBox();
		genBox.setWidth("157px");
		//		layoutPanel.add(genBox);
		//		layoutPanel.setWidgetLeftWidth(genBox, 137.0, Unit.PX, 165.0, Unit.PX);
		//		layoutPanel.setWidgetTopHeight(genBox, 121.0, Unit.PX, 32.0, Unit.PX);
		//		genBox.setVisible(false);
		//		genBox.setEnabled(false);


		btnSplit.setVisible(false);

		btnSplit.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				AsyncCallback<String> splitCallback = new AsyncCallback<String>(){

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(String result) {
						rp.GetEvents();
						pp.Refresh();
					}

				};
				rp.rpsvc.SplitRaid(evt, splitCallback);
				hide();
			}
		});

		chckbxCancel.setVisible(false);

		gameBox.addChangeHandler(new ChangeHandler(){

			@Override
			public void onChange(ChangeEvent event) {
				if(rp.destlist == null) return;
				String game = gameboxitems.get(gameBox.getSelectedIndex());
				pointspool pp = poolitems.get(gameBox.getSelectedIndex());
				destBox.clear(); secdestBox.clear();
				ArrayList<String> dest = rp.destlist.get(game);
				if(dest == null) return;
				for(int i = 1; i < dest.size(); i++) {
					String ds = dest.get(i);
					if(!pp.dests.contains(ds)) continue;
					destBox.addItem(ds); secdestBox.addItem(ds);
				}
				setsdest();
			}

		});

		destBox.addChangeHandler(new ChangeHandler(){

			@Override
			public void onChange(ChangeEvent event) {
				setsdest();				
			}

		});

		groupBox.addChangeHandler(new ChangeHandler(){

			@Override
			public void onChange(ChangeEvent event) {
				popPools();				
			}

		});

		AsyncCallback<HashMap<String,ArrayList<pointspool>>> charterCallback = new AsyncCallback<HashMap<String,ArrayList<pointspool>>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(HashMap<String, ArrayList<pointspool>> result) {
				charterList = result;
				groupBox.clear();
				for(String s : charterList.keySet()){
					groupBox.addItem(s);
					if(evt != null && !evt.game.equals("")){
						Iterator<pointspool> it = charterList.get(s).iterator();
						while(it.hasNext()){
							pointspool tp = it.next();
							if(!tp.dests.contains(evt.dest)) it.remove();
						}
					}
				}
				
				boolean cg = false;
				if(evt == null) cg = true;
				else{
					if(evt.game.equals("")) cg = true;
				}
				if(rp.auth == RaidPoints.AuthType.admin && cg) groupBox.addItem("General"); 

				if(rp.destlist == null){
					gameCallback = new AsyncCallback<String>(){

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(String result) {
							rp.destCallback.onSuccess(result);
							if(evt != null){
								for(int i = 0; i < groupBox.getItemCount(); i++){
									//GWT.log(groupBox.getItemText(i));
									if(groupBox.getItemText(i).equals(evt.group)) {groupBox.setSelectedIndex(i); break;} 
								}
							}
							popPools();
							//popGame();
							if(evt != null) popExist();

						}

					};
					rp.rpsvc.GetDest(gameCallback);
				}
				else{
					if(evt != null){
						for(int i = 0; i < groupBox.getItemCount(); i++){
							//GWT.log(groupBox.getItemText(i));
							if(groupBox.getItemText(i).equals(evt.group)) {groupBox.setSelectedIndex(i); break;} 
						}
					}
					popPools();
					//popGame();
					if(evt != null) popExist();
				}

			}

		};
		rp.rpsvc.GetCharters(charterCallback);		


	}

	public void popExist(){
		String game = "";
		pointspool pp = null;
		if(evt.game.equals("")){
			for(int i = 0; i < groupBox.getItemCount(); i++){
				if(groupBox.getItemText(i).equals("General")) {groupBox.setSelectedIndex(i); break;}
			}
			genBox.setText(evt.note.split(":;:")[0]);
			noteBox.setText(evt.note.split(":;:")[1]);
			popPools();
		}
		else{
			for(int i = 0; i < poolid.size(); i++){
				if(poolid.get(i) == evt.poolid){
					gameBox.setSelectedIndex(i);
					game = gameboxitems.get(i);
					pp = poolitems.get(i);
					break;
				}
			}
			//		for(int i = 0; i < gameBox.getItemCount(); i++){
			//			if(gameBox.getItemText(i).equals(evt.game)) {gameBox.setSelectedIndex(i); break;} 
			//		}
			destBox.clear(); secdestBox.clear();
			ArrayList<String> dest = rp.destlist.get(game);
			if(dest == null) return;
			for(int i = 1; i < dest.size(); i++) {
				String ds = dest.get(i);
				if(!pp.dests.contains(ds)) continue;
				destBox.addItem(ds); secdestBox.addItem(ds);
			}
			for(int i = 0; i < destBox.getItemCount(); i++){
				if(destBox.getItemText(i).equals(evt.dest)) {destBox.setSelectedIndex(i); break;} 
			}
			setsdest();
			for(int i = 0; i < secdestBox.getItemCount(); i++){
				if(evt.secdest.contains(secdestBox.getItemText(i))) secdestBox.setItemSelected(i, true);
			}
			noteBox.setText(evt.note);
		}

		startBox.setText(evt.starttime.getTime()); endBox.setText(evt.endtime.getTime());

		java.util.Date dtt = new java.util.Date();
		dtt.setMonth(evt.starttime.month - 1);
		dtt.setYear(evt.starttime.year);
		dtt.setDate(evt.starttime.day);
		dateBox.setValue(dtt);
		lblTimesToReoccur.setVisible(false); integerBox.setVisible(false);
		if(!evt.over) {
			chckbxCancel.setVisible(true);
			if(evt.split <= 0 && !evt.game.equals("")) btnSplit.setVisible(true);
		} 
		btnAdd.setText("Update");
	}

	public boolean timevalid(String time){
		if(time.matches("[0-2]{0,1}[0-9]{0,1}[:]{0,1}[0-5]{0,1}[0-9]{0,1}")) return true;
		else return false;
	}

	public void setsdest(){
		for(int i = 0; i < secdestBox.getItemCount(); i++){
			secdestBox.setItemSelected(i, false);
			if(i==destBox.getSelectedIndex()) secdestBox.getElement().getElementsByTagName("option").getItem(i).setAttribute("disabled","disabled");
			else secdestBox.getElement().getElementsByTagName("option").getItem(i).removeAttribute("disabled");
		}
	}

	public void popPools(){
		chartergid.clear();
		charterpoolname.clear();
		poolid.clear();
		gameBox.clear();
		gameboxitems.clear();
		destBox.clear();
		secdestBox.clear();

		if(groupBox.getItemCount() > 0 && groupBox.getItemText(groupBox.getSelectedIndex()).equals("General")){
			if(!isgen){
				isgen = true;
				layoutPanel.remove(destBox);
				layoutPanel.add(genBox);
				layoutPanel.setWidgetLeftWidth(genBox, 141.0, Unit.PX, 177.0, Unit.PX);
				layoutPanel.setWidgetTopHeight(genBox, 123.0, Unit.PX, 24.0, Unit.PX);
				lblSecDestination.setVisible(false);
				secdestBox.setVisible(false);
				lblPointsPool.setVisible(false);
				gameBox.setVisible(false);
				lblNewLabel.setText("Event Title:");
			}

		}
		else{
			if(isgen){
				isgen = false;
				layoutPanel.remove(genBox);
				layoutPanel.add(destBox);
				layoutPanel.setWidgetLeftWidth(destBox, 141.0, Unit.PX, 177.0, Unit.PX);
				layoutPanel.setWidgetTopHeight(destBox, 123.0, Unit.PX, 24.0, Unit.PX);
				lblSecDestination.setVisible(true);
				secdestBox.setVisible(true);
				lblPointsPool.setVisible(true);
				gameBox.setVisible(true);
				lblNewLabel.setText("Prim. Destination:");
			}
			for(String s : charterList.keySet()){
				if(s.equals(groupBox.getItemText(groupBox.getSelectedIndex()))){
					poolitems.clear();					
					for(pointspool ss : charterList.get(s)){
						poolitems.add(ss);
						charterpoolname.add(ss.name);
						poolid.add(ss.id);
						chartergid.add(ss.gid);
						Iterator<String> games = rp.destlist.keySet().iterator();
						while(games.hasNext()) {
							String gs = games.next();
							int gid = Integer.parseInt(rp.destlist.get(gs).get(0));
							if(gid == ss.gid){
								gameBox.addItem(gs + " - " + ss.name);
								gameboxitems.add(gs);
								break;
							}
						}
					}
				}
			}
			popGame();
		}

	}

	public void popGame(){
		//		if(rp.destlist == null) return;
		//		Iterator<String> games = rp.destlist.keySet().iterator();
		//		gameBox.clear();
		//		while(games.hasNext()) {
		//			String gs = games.next();
		//			int gid = Integer.parseInt(rp.destlist.get(gs).get(0));
		//			gameBox.addItem(gs);
		//		}
		if(gameBox.getItemCount() > 0) gameBox.setSelectedIndex(0);
		for(int i = 0; i < gameboxitems.size(); i++){
			if(gameboxitems.get(i).equals("World of Warcraft")) {gameBox.setSelectedIndex(i); break;}
		}
		String game = gameboxitems.get(gameBox.getSelectedIndex());
		pointspool pp = poolitems.get(gameBox.getSelectedIndex());
		destBox.clear();
		secdestBox.clear();
		ArrayList<String> dest = rp.destlist.get(game);
		if(dest == null) return;
		for(int i = 1; i < dest.size(); i++) {
			String ds = dest.get(i);
			if(!pp.dests.contains(ds)) continue;
			destBox.addItem(ds);
			secdestBox.addItem(ds);
		}
		setsdest();
	}

	public static class pointspool implements Serializable{
		public int gid=0; public int id = 0; public String name=""; public ArrayList<String> dests=null;
	}
}
